#include<stdlib.h>
typedef int DataType;
typedef struct QNode
{
    DataType data;
    struct QNode * next;
}LQNode,*QueuePtr;

typedef struct 
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

void InitQueue(LinkQueue *LQ)
{
    LQ->front = LQ->rear = (QueuePtr)malloc(sizeof(LQNode));
    if(LQ->front == NULL)
        exit(-1);
    LQ->front->next = NULL;
}

int QueueEmpty(LinkQueue *LQ)
{
    if(LQ->front == LQ->rear)
        return 1;
    else
        return 0;
}

int EnQueue(LinkQueue *LQ, DataType e)
{
    QueuePtr pt = (QueuePtr)malloc(sizeof(LQNode));
    if(pt == NULL) return 0;
    pt->data = e;
    LQ->rear->next = pt;
    LQ->rear = LQ->rear->next;
    return 1;
}

int DeQueue(LinkQueue *LQ, DataType *e)
{
    if(LQ->front == LQ->rear) return 0;
    QueuePtr pt = LQ->front->next;
    *e = pt->data;
    LQ->front->next = pt->next;
    if(pt == LQ->rear) LQ->rear = LQ->front;
    free(pt);
    return 1;
}

void DisplayQueue(LinkQueue *LQ);

int GetHead(LinkQueue *LQ,DataType *e)
{
    if(LQ->front == LQ->rear) return 0;
    *e = LQ->front->next->data;
    return 1;
}

void ClearQueue(LinkQueue *LQ)
{
    QueuePtr pt;
    while(LQ->front != LQ->rear)
    {
        pt = LQ->front->next;
        if(LQ->rear == pt) LQ->rear = LQ->front;
        LQ->front->next = pt->next;
        free(pt);
    }
}
